package com.hifar.test.dailydisplacement.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hifar.common.QueryParamBean;
import com.hifar.model.VO.QueryParamsVO;
import com.hifar.sys.BusiResponse;
import com.hifar.sys.PageResult;
import com.hifar.sys.SystemLoginUser;
import com.hifar.sys.SystemUtil;
import com.hifar.test.dailydisplacement.pojo.HfDailyDisplacement;
import com.hifar.test.dailydisplacement.service.IHfDailyDisplacementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 日常位移记录 前端控制器
 * </p>
 *
 * @author system
 * @since 2025-10-15
 */
@RestController
@RequestMapping("/api/busi/test/hfDailyDisplacementController")
@Api(tags = "日常位移管理功能")
public class HfDailyDisplacementController {

    @Autowired
    private IHfDailyDisplacementService hfDailyDisplacementService;

    /**
     * 分页查询列表
     */
    @PostMapping(value = "/list")
    @ApiOperation(value = "日常位移管理:分页查询列表")
    public BusiResponse list(@RequestBody @Validated QueryParamsVO queryParamsVO) {
        QueryParamBean<HfDailyDisplacement> queryParamBean = new QueryParamBean<>(queryParamsVO);
        queryParamBean.addColumns(HfDailyDisplacement.class);
        QueryWrapper<HfDailyDisplacement> queryWrapper = queryParamBean.getQueryWrapper();
        Page<HfDailyDisplacement> page = queryParamBean.getPage();
        
        // 权限控制：密级过滤
        queryWrapper.in("secret_level", SystemLoginUser.dataSecretSet);
        // 默认排序：按创建时间倒序
        queryWrapper.orderByDesc("create_time");
        
        Page<HfDailyDisplacement> pageData = hfDailyDisplacementService.page(page, queryWrapper);
        
        /* 计算分页信息返回给前端 */
        PageResult pageResult = SystemUtil.parsePage(page, pageData);
        return BusiResponse.OK(pageResult);
    }

    /**
     * 新增或编辑
     */
    @PostMapping(value = "/save")
    @ApiOperation(value = "日常位移管理:新增或编辑")
    public BusiResponse save(@RequestBody @Validated HfDailyDisplacement hfDailyDisplacement) {
        // 检查是否存在相同日期和类型的记录
        boolean isDuplicate = hfDailyDisplacementService.checkDuplicate(
            hfDailyDisplacement.getEntrustTime(),
            hfDailyDisplacement.getUnitTypeCode(),
            hfDailyDisplacement.getId()
        );
        
        if (isDuplicate) {
            String typeText = "1".equals(hfDailyDisplacement.getUnitTypeCode()) ? "气候" : "力学";
            return BusiResponse.error(500,"该日期的" + typeText + "类型记录已存在，不可重复添加！");
        }
        
        boolean result = hfDailyDisplacementService.saveOrUpdate(hfDailyDisplacement);
        if (result) {
            return BusiResponse.ok("操作成功！");
        } else {
            return BusiResponse.error(500,"操作失败！");
        }
    }

    /**
     * 根据id查询数据
     */
    @GetMapping("/queryById")
    @ApiOperation(value = "日常位移管理:根据id查询数据")
    public BusiResponse queryById(@RequestParam("id") String id) {
        HfDailyDisplacement record = hfDailyDisplacementService.getById(id);
        return BusiResponse.OK(record);
    }

    /**
     * 批量删除（逻辑删除）
     */
    @PostMapping("/delete")
    @ApiOperation(value = "日常位移管理:批量删除")
    public BusiResponse delete(@RequestBody List<String> idList) {
        if (idList == null || idList.isEmpty()) {
            return BusiResponse.error("请至少选择一条数据！");
        }
        
        boolean result = hfDailyDisplacementService.removeByIds(idList);
        if (result) {
            return BusiResponse.ok("删除成功！");
        } else {
            return BusiResponse.error("删除失败！");
        }
    }
}

